﻿/**
 * Grid for displaying Consolidation details
 */
Ext.define('Sisconsolid.view.consolidation.List', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.consolidation.list',
    requires: [
        'Ext.grid.column.Boolean',
        'Ext.grid.column.Date',
		'Ext.grid.column.Action',
		'Sisconsolid.model.Tracing',
		'Sisconsolid.controller.Tracing'
    ],
    title: translations.consolidationsPanelTitle,
    iconCls: 'icon_list',
    store: 'Consolidations',
    initComponent: function() {
        var me = this;
        Ext.applyIf(me, {
            columns: {
                defaults: {},
                items: [
                    {
                        xtype: 'datecolumn',
						text: translations.date,
                        dataIndex: 'date',
						format: translations.dateFormat
                    },
                    {
                        text: translations.firstAndLastName,
                        dataIndex: 'firstName',
                        renderer: function( value, metaData, record, rowIndex, colIndex, store, view ) {
                            return value + ', ' + record.get( 'lastName' )
                        },
                        width: 200
                    },
                    {
                        text: translations.address,
                        dataIndex: 'prittyAddress',
                        width: 200
                    },
                    {
                        text: translations.personalPhone,
                        dataIndex: 'personalPhone'
                    },
                    {
                        text: translations.assignedTo,
                        dataIndex: 'assignedTo'
                    },
                    {
                        text: translations.tookData,
                        dataIndex: 'tookData',
						hidden: true
                    },
                    {
                        text: translations.years,
                        dataIndex: 'years',
                        hidden: true
                    },
                    {
                        text: translations.civilStatus,
                        dataIndex: 'civilStatusDescr',
                        hidden: true
                    },
                    {
                        text: translations.cell,
							
                        hidden: true
                    }, 
					{
						text: translations.tracings,
						columns: [{
							xtype:'actioncolumn',
							tdCls:'view',
							text     : translations.phoneVisit1,
							width    : 40,
							align: 'center',
							items: [
								{
									getClass: function(v, meta, rec) {
										return me.getIconLoadedOwed(rec, 'phv1Id', 'phv1Owed');
									}
								},
								{
									getClass: function(v, meta, rec) {
										return me.getButtonUpdateTracing(rec, 'phv1Id');
									},
									handler: function(grid, rowIndex, colindex) {
										var consolidationRecord = grid.getStore().getAt(rowIndex);										
										me.processTracing(consolidationRecord.data.phv1Id,
														  consolidationRecord.data.idConsolidation,
														  consolidationRecord.data.phv1IdTracingType,
														  consolidationRecord.data.phv1TracingType,
														  consolidationRecord.data.need,
														  consolidationRecord.data.idCell);
 									}
								}
							]
						},
						{
							xtype:'actioncolumn',
							text     : translations.visit1,
							width    : 40,
							align: 'center',
							items: [{
									getClass: function(v, meta, rec) {
										return me.getIconLoadedOwed(rec, 'v1Id', 'v1Owed');
									}
								},
								{
									getClass: function(v, meta, rec) {
										return me.getButtonUpdateTracing(rec, 'v1Id');
									},
									handler: function(grid, rowIndex, colindex) {
										var consolidationRecord = grid.getStore().getAt(rowIndex);										
										me.processTracing(consolidationRecord.data.v1Id,
														  consolidationRecord.data.idConsolidation,
														  consolidationRecord.data.v1IdTracingType,
														  consolidationRecord.data.v1TracingType,
														  consolidationRecord.data.need,
														  consolidationRecord.data.idCell);
 									}
								}
							]
						}, 
						{
							xtype:'actioncolumn',
							tdCls:'view',
							text     : translations.phoneVisit2,
							width    : 40,
							align: 'center',
							items: [{
									getClass: function(v, meta, rec) {
										return me.getIconLoadedOwed(rec, 'phv2Id', 'phv2Owed');
									}
								},
								{
									getClass: function(v, meta, rec) {
										return me.getButtonUpdateTracing(rec, 'phv2Id');
									},
									handler: function(grid, rowIndex, colindex) {
										var consolidationRecord = grid.getStore().getAt(rowIndex);										
										me.processTracing(consolidationRecord.data.phv2Id,
														  consolidationRecord.data.idConsolidation,
														  consolidationRecord.data.phv2IdTracingType,
														  consolidationRecord.data.phv2TracingType,
														  consolidationRecord.data.need,
														  consolidationRecord.data.idCell);
 									}
								}
							]
						},						 
						{
							xtype:'actioncolumn',
							tdCls:'view',
							text     : translations.visit2,
							width    : 40,
							align: 'center',
							items: [{
									getClass: function(v, meta, rec) {
										return me.getIconLoadedOwed(rec, 'v2Id', 'v2Owed');
									}
								},
								{
									getClass: function(v, meta, rec) {
										return me.getButtonUpdateTracing(rec, 'v2Id');
									},
									handler: function(grid, rowIndex, colindex) {
										var consolidationRecord = grid.getStore().getAt(rowIndex);										
										me.processTracing(consolidationRecord.data.v2Id,
														  consolidationRecord.data.idConsolidation,
														  consolidationRecord.data.v2IdTracingType,
														  consolidationRecord.data.v2TracingType,
														  consolidationRecord.data.need,
														  consolidationRecord.data.idCell);
 									}
								}
							]
						}, 
						{
							xtype:'actioncolumn',
							tdCls:'view',
							text     : translations.visit3,
							width    : 40,
							align: 'center',
							items: [{
									getClass: function(v, meta, rec) {
										return me.getIconLoadedOwed(rec, 'v3Id', 'v3Owed');
									}
								},
								{
									getClass: function(v, meta, rec) {
										return me.getButtonUpdateTracing(rec, 'v3Id');
									},
									handler: function(grid, rowIndex, colindex) {
										var consolidationRecord = grid.getStore().getAt(rowIndex);										
										me.processTracing(consolidationRecord.data.v3Id,
														  consolidationRecord.data.idConsolidation,
														  consolidationRecord.data.v3IdTracingType,
														  consolidationRecord.data.v3TracingType,
														  consolidationRecord.data.need,
														  consolidationRecord.data.idCell);
 									}
								}
							]
						}, 
						{
							xtype:'actioncolumn',
							tdCls:'view',
							text     : translations.visit4,
							width    : 40,
							align: 'center',
							items: [{
									getClass: function(v, meta, rec) {
										return me.getIconLoadedOwed(rec, 'v4Id', 'v4Owed');
									}
								},
								{
									getClass: function(v, meta, rec) {
										return me.getButtonUpdateTracing(rec, 'v4Id');
									},
									handler: function(grid, rowIndex, colindex) {
										var consolidationRecord = grid.getStore().getAt(rowIndex);										
										me.processTracing(consolidationRecord.data.v4Id,
														  consolidationRecord.data.idConsolidation,
														  consolidationRecord.data.v4IdTracingType,
														  consolidationRecord.data.v4TracingType,
														  consolidationRecord.data.need,
														  consolidationRecord.data.idCell);
 									}
								}
							]
						}]
					}, 
					{
						xtype:'actioncolumn',
						tdCls:'view',
						text     : translations.view,
						width    : 40,
						align: 'center',						
						items: [{
								iconCls: 'icon_pdf',
								handler: function(grid, rowIndex, colindex) {
									var consolidationRecord = grid.getStore().getAt(rowIndex);										
									new Ext.Window({
									title:  translations.consolidation,
									width:  840,
									height: 580,
									layout: 'fit',
									items:  [{
											xtype: "component",
											autoEl: {
												id:'ifrmConsolidationCard',
												tag: "iframe",
												scrolling: 'no',
												seamless: 'seamless',
												frameborder: 0,
												width: '100%',
												height: '100%',
												type: 'application/pdf',
												src: '/archives/sisconsolid/consolidation/' + consolidationRecord.data.idConsolidation + '.pdf'
											},
											setSrc: function(src) {
												if (this.rendered) {
													Ext.get('ifrmConsolidationCard').set({
														src: src
													});													
												} else {
													this.autoEl.src = src;
												}
												Ext.get('ifrmConsolidationCard').reload();
											}
										}]
									}).show();
								}
							}
						]
					}
                ]
            },
            dockedItems: [
                {
                    xtype: 'toolbar',
                    dock: 'top',
                    ui: 'footer',
                    items: [
                        {
                            xtype: 'button',
                            itemId: 'add',
                            iconCls: 'icon_add',
                            text: translations.addConsolidation
                        },
						{
                            xtype: 'button',
                            itemId: 'search',
                            iconCls: 'icon_search',
                            text: translations.searchConsolidations
                        },
						{
                            xtype: 'button',
                            itemId: 'clear',
                            iconCls: 'icon_clear',
                            text: translations.clearSearch
                        }
                    ]
                },
                {
                    xtype: 'pagingtoolbar',
                    ui: 'footer',
                    defaultButtonUI: 'default',
                    dock: 'bottom',
                    displayInfo: true,
                    store: me.getStore()
                }
            ]
        });
        me.callParent( arguments );
    },	
	getButtonUpdateTracing: function(rec, tracingId) {
		if(rec.get(tracingId) != null) {
			return'icon_pencil';
		}
		return 'icon_document';	
	},	
	getIconLoadedOwed: function(rec, tracingId, owed) {
		if ( rec.get(tracingId) != null) {
			return 'icon_tick';
		} else {
			if (rec.get(owed)) {
				return 'icon_warning';
			}
		}
		return 'x-hide-display';
	},
	processTracing: function(idTracing, idConsolidation, idTracingType, tracingType, need, idCell) {
		var tracingController = Ext.create('Sisconsolid.controller.Tracing');
		tracingController.processTracing(idTracing, idConsolidation, idTracingType, tracingType, need, idCell);			
	}
});